﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TA.Win.Utils.Design
{
	/// <summary>
	/// Allows editing a collection strings.
	/// </summary>
	public partial class StringCollectionDialog : Form
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public StringCollectionDialog()
		{
			InitializeComponent();
			this.Options = new StringOptions();
		}
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		/// <summary>
		/// Gets or sets the options which control how this form operates.
		/// </summary>
		public StringOptions Options
		{
			get
			{
				return this._options;
			}
			set
			{
				this._options = value;

				this.UpdateComboBox();
				this.UpdateOptions();
			}
		}
		/// <summary>
		/// Gets or sets the array of strings which is edited by this form.
		/// </summary>
		public string[] Value
		{
			get
			{
				List<string> res = new List<string>();
				foreach (string s in this.lb.Items)
				{
					res.Add(s);
				}
				return res.ToArray();
			}
			set
			{
				this.lb.Items.Clear();
				foreach (string s in value)
				{
					this.lb.Items.Add(s);
				}
				this.UpdateOptions();
			}
		}
		#endregion
		#region METHODS
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		StringOptions _options = new StringOptions();
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		#endregion
		#region HANDLERS
		private void btnAdd_Click(object sender, EventArgs e)
		{
			string value = this.cmb.Text;
			if (this.CanBeAdded(value))
			{
				lb.Items.Add(value);
				this.cmb.Text = "";
				this.UpdateOptions();
			}
			this.cmb.Focus();
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			while (this.lb.SelectedItems.Count > 0)
				this.lb.Items.Remove(this.lb.SelectedItems[0]);
			this.UpdateOptions();
		}
		private void cmb_Enter(object sender, EventArgs e)
		{
			this.UpdateAcceptButtonComboBox();
		}
		private void cmb_TextChanged(object sender, EventArgs e)
		{
			this.UpdateAcceptButtonComboBox();
		}
		private void cmb_Leave(object sender, EventArgs e)
		{
			this.AcceptButton = this.btnOk;
		}
		private void lb_Enter(object sender, EventArgs e)
		{
			this.AcceptButton = this.btnRemove;
		}
		private void lb_Leave(object sender, EventArgs e)
		{
			this.btnRemove = this.btnOk;
		}
		#endregion
		#region DISPLAY
		private void UpdateAcceptButtonComboBox()
		{
			this.AcceptButton = this.cmb.Text.Length > 0 ? this.btnAdd : this.btnOk;
		}
		private void UpdateOptions()
		{
			this.cmb.Items.Clear();
			foreach (string oneOption in this.Options.Options)
			{
				if (this.CanBeAdded(oneOption))
					this.cmb.Items.Add(oneOption);
			}
		}
		private void UpdateComboBox()
		{
			this.cmb.DropDownStyle = this.Options.CustomValueAllowed ? ComboBoxStyle.DropDown : ComboBoxStyle.DropDownList;
		}
		private bool CanBeAdded(string value)
		{
			return this.Options.DuplicatesAllowed || !this.IsItemAdded(value);
		}
		private bool IsItemAdded(string value)
		{
			foreach (string item in this.lb.Items)
			{
				if (string.Equals(item, value, this.Options.ValueComparison))
				{
					return true;
				}
			}
			return false;
		}
		#endregion
		#endregion
		#endregion
	}
}
